from mininet.net import Mininet
from mininet.topo import Topo
from mininet.log import setLogLevel
import os


class CustomTopo(Topo):
    def build(self, *args, **params):
        h1 = self.addHost('h1')
        h2 = self.addHost('h2')
        s1 = self.addSwitch('s1')

        self.addLink(h1, s1)
        self.addLink(s1, h2)


def main():
    topo = CustomTopo()
    net = Mininet(topo=topo)
    net.start()
    h2_cmd = os.system("sudo nohup mx h2 iperf -s &")
    h1_cmd = os.popen("sudo mx h1 iperf -c 10.0.0.2")
    result = h1_cmd.read()
    print(result.split("\n")[6])
    h1_cmd.close()
    net.stop()


if __name__ == '__main__':
    setLogLevel('error')
    main()
